package cn.com.shopec.erp.base.service.impl;
import java.lang.reflect.InvocationTargetException;
import java.util.ArrayList;
import java.util.List;
import javax.annotation.Resource;
import org.apache.commons.beanutils.PropertyUtils;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.MatchMode;
import org.hibernate.criterion.Restrictions;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import cn.com.shopec.erp.base.dao.ILogisticsInfoDao;
import cn.com.shopec.erp.base.dao.ILogisticsSettleDao;
import cn.com.shopec.erp.base.entity.LogisticsInfo;
import cn.com.shopec.erp.base.entity.LogisticsSettle;
import cn.com.shopec.erp.base.entity.SupplierInfo;
import cn.com.shopec.erp.base.service.ILogisticInfoService;
import cn.com.shopec.erp.base.service.ILogisticSettleService;
import cn.com.shopec.erp.base.vo.LogisticInfoVo;
import cn.com.shopec.erp.common.constant.SZSCMConstant;
import cn.com.shopec.erp.common.page.PageFinder;
import cn.com.shopec.erp.common.page.PageQuery;
import cn.com.shopec.erp.common.utils.LogicUtil;

@Service
public class LogisticInfoServicImpl implements ILogisticInfoService
{
     @Resource
     private ILogisticsInfoDao logisticsInfoDao;
     @Resource
     private ILogisticsSettleDao logisticsSettleDao;
     @Resource
     private ILogisticSettleService logisticSettleService;
     
     @Override
     public PageFinder<LogisticsInfo> getPageFinderLogisticInfo(LogisticsInfo logisticsInfo, PageQuery pageQuery)
     {
          DetachedCriteria dc = logisticsInfoDao.createCriteria();
          dc.add(Restrictions.eq("deleteFlag", SZSCMConstant.DELETE_FLAG_NO));
          if (null != logisticsInfo)
          {
               if (LogicUtil.isNotNullAndEmpty(logisticsInfo.getLogisticsName()))
               {
                    dc.add(Restrictions.like("logisticsName", logisticsInfo.getLogisticsName(), MatchMode.ANYWHERE));
               }
          }
          return logisticsInfoDao.pagedByCriteria(dc, pageQuery.getPage(), pageQuery.getPageSize());
     }
     
     
     @Override
     @Transactional
     public LogisticsInfo addLogisticInfo(LogisticsInfo logisticsInfo) throws Exception
     {
          LogisticsInfo info = new LogisticsInfo();
          info.setCode(SZSCMConstant.genUUID());// code
          info.setDeleteFlag(SZSCMConstant.DELETE_FLAG_NO);// 未删除标识
          info.setLogisticsName(logisticsInfo.getLogisticsName());// 物流名称
          info.setLogisticsId(logisticsInfo.getLogisticsId());// 物流编码
          info.setDescribe(logisticsInfo.getDescribe());// 描述
          return logisticsInfoDao.save(info);
     }
     
     @Override
     public LogisticsInfo updateLogisticsInfo(LogisticsInfo logisticsInfo) throws Exception
     {
          LogisticsInfo info = getLogisticsInfoByCode(logisticsInfo.getCode());
          info.setCode(logisticsInfo.getCode());
          info.setDeleteFlag(SZSCMConstant.DELETE_FLAG_NO);
          info.setLogisticsName(logisticsInfo.getLogisticsName());// 物流名称
          info.setLogisticsId(logisticsInfo.getLogisticsId());// 物流编码
          info.setDescribe(logisticsInfo.getDescribe());// 描述
          return logisticsInfoDao.update(info);
     }
     
     @Override
     public LogisticsInfo getLogisticsInfoByCode(String code)
     {
          return logisticsInfoDao.findUniqueBy("code", code);
     }
     
     @Override
     public List<LogisticInfoVo> getAllLogisticInfo(LogisticInfoVo vo) throws IllegalAccessException, InvocationTargetException, NoSuchMethodException
     {
          DetachedCriteria de = logisticsSettleDao.createCriteria();
          de.add(Restrictions.eq("deleteFlag", SZSCMConstant.DELETE_FLAG_NO));
          de.add(Restrictions.eq("settleType", vo.getSettleType()));
          List<LogisticsSettle> listInfo = logisticsSettleDao.findByCriteria(de);
          List<LogisticInfoVo> vos = new ArrayList<LogisticInfoVo>();
          if (null != listInfo && listInfo.size() > 0)
          {
               for (LogisticsSettle info : listInfo)
               {
                    LogisticInfoVo vo1 = new LogisticInfoVo();
                    PropertyUtils.copyProperties(vo1, info);
                    try
                    {
                         String code = info.getCode();
                         LogisticsInfo li = getLogisticsInfoByCode(code);
                         vo1.setLogisticsName(li.getLogisticsName());// 物流名称
                         vo1.setLogisticsId(li.getLogisticsId());// 物流编号
                         vo1.setDescribe(li.getDescribe());
                         vo1.setPoundage(info.getPoundage());
                    }
                    catch (Exception e)
                    {
                    }
                    vos.add(vo1);
               }
          }
          return vos;
     }
     
     
     @Override
 	public LogisticInfoVo getLogisticByLogisId(String logisticCode) throws Exception {
         DetachedCriteria de = logisticsInfoDao.createCriteria();
         de.add(Restrictions.eq("deleteFlag", SZSCMConstant.DELETE_FLAG_NO));
         de.add(Restrictions.eq("logisticsId", logisticCode));
         List<LogisticsInfo>  infos = logisticsInfoDao.findByCriteria(de);
         if(null ==null || infos.size()  == 0)
         {
        	 return null;
         }
         LogisticInfoVo logisticInfoVo = new LogisticInfoVo();
         LogisticsInfo logisticsInfo = infos.get(0);
         logisticInfoVo.setLogisticsName(logisticsInfo.getLogisticsName());// 物流名称
         logisticInfoVo.setLogisticsId(logisticsInfo.getLogisticsId());// 物流编号
         logisticInfoVo.setDescribe(logisticsInfo.getDescribe());
         LogisticsSettle logisticsSettle = logisticSettleService.getLogisticsSettleByCode(logisticsInfo.getCode(), "3").get(0);
         logisticInfoVo.setPoundage(logisticsSettle.getPoundage());    
         PropertyUtils.copyProperties(logisticInfoVo, logisticsSettle);
 		return logisticInfoVo;
 	}
     
     @Override
     @Transactional
     public void delLogisticInfo(String code) throws Exception
     {
          LogisticsInfo info = getLogisticsInfoByCode(code);
          List<LogisticsSettle> list = logisticSettleService.getAllLogisticsSettleByCode(code);
          if (null != list && list.size() > 0)
          {
               for (LogisticsSettle set : list)
               {
                    set.setDeleteFlag(SZSCMConstant.DELETE_FLAG_YES);// 已删除
                    logisticsSettleDao.update(set);
               }
               info.setDeleteFlag(SZSCMConstant.DELETE_FLAG_YES);
          }
          else
          {
               info.setDeleteFlag(SZSCMConstant.DELETE_FLAG_YES);
          }
          logisticsInfoDao.update(info);
     }
     
     
     public List<LogisticsInfo> getLogisticInfoche(String logisticsId)
     {
          DetachedCriteria detachedCriteria = logisticsInfoDao.createCriteria();
          detachedCriteria.add(Restrictions.eq("logisticsId", logisticsId));
          detachedCriteria.add(Restrictions.eq("deleteFlag", SZSCMConstant.DELETE_FLAG_NO));
          return logisticsInfoDao.findByCriteria(detachedCriteria);
     }

     public List<LogisticsInfo> getLogisticsByName(String logisticsName)
     {
          DetachedCriteria detachedCriteria = logisticsInfoDao.createCriteria();
          detachedCriteria.add(Restrictions.eq("logisticsName", logisticsName));
          detachedCriteria.add(Restrictions.eq("deleteFlag", SZSCMConstant.DELETE_FLAG_NO));
          return logisticsInfoDao.findByCriteria(detachedCriteria);
     }
}
